package com.itboye.lottery.utils;

import android.text.TextUtils;

import com.itboye.lottery.bean.BallInfoEntity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;

import static com.itboye.lottery.utils.BaskcketballPrizeH.*;

public class DisplayBetContent {
    public static String[] getBetContentAndPrizeContent(String plan, BallInfoEntity ballInfoEntity, int type) {
        String[] values = new String[]{"", "", "", ""};
        JSONObject jsonObject = null;
        String sf = "", sf_tag = "", rsf = "", rsfTag = "", dxf = "", dxf_tag = "", sfc = "", sfc_tag = "";
        try {
            String bDate = ballInfoEntity.getB_date().replaceAll("-", "");
            values[2] = bDate;
            if (!TextUtils.isEmpty(plan)) {
                jsonObject = new JSONObject(plan);
                String num = ballInfoEntity.getNum_type();
                JSONObject jsonObject1 = jsonObject.getJSONObject(bDate + num);
                Iterator<String> iterator = jsonObject1.keys();
                HashMap<String, String> hashMap = new HashMap<>();
                while (iterator.hasNext()) {
                    StringBuffer stringBufferValue = new StringBuffer();
                    StringBuffer kaijiang = new StringBuffer();
                    String key = iterator.next();
                    hashMap.put(key, key);
                    JSONObject jsonObjectValue = jsonObject1.getJSONObject(key);
                    Iterator<String> iterator2 = jsonObjectValue.keys();
                    // 3010: 胜平负
                    // 3006:让球胜平负
                    // 3007: 全场比分(90分钟)
                    // 3008: 进球数
                    // 3009: 半全场胜负  这个暂时不支持
                    // 3011: 混合过关 （一注投注中包含2个玩法以上的就是混合过关）
                    switch (key) {
                        case "3010":
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                if (key2.equals("0")) {
                                    stringBufferValue.append("主负,");
                                }
                                if (key2.equals("1")) {
                                    stringBufferValue.append("主平,");
                                }
                                if (key2.equals("3")) {
                                    stringBufferValue.append("主胜,");
                                }
                            }
                            if (!TextUtils.isEmpty(ballInfoEntity.getFinal_score())) {
                                if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) > Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                                    //主胜
                                    kaijiang.append("主胜,");
                                } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) == Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                                    //主胜
                                    kaijiang.append("主平,");
                                } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) < Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                                    //主胜
                                    kaijiang.append("主负,");
                                }
                            }
                            break;
                        case "3007"://全场比分
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                if (!key2.equals("single")) {
                                    JSONObject value = jsonObjectValue.getJSONObject(key2);//种类所带表的赔率
                                    Iterator<String> iterator3 = value.keys();
                                    while (iterator3.hasNext()) {
                                        String key3 = iterator3.next();
                                        if (key3.equals("90")) {
                                            stringBufferValue.append("胜其它,");
                                        }else if(key3.equals("99")){
                                            stringBufferValue.append("平其它,");
                                        }else if(key3.equals("09")){
                                            stringBufferValue.append("负其它,");
                                        }else {
                                            stringBufferValue.append(key3.substring(0, 1) + ":" + key3.substring(1, 2) + ",");
                                        }
                                    }
                                }
                            }
                            if (!TextUtils.isEmpty(ballInfoEntity.getFinal_score())) {
                                kaijiang.append(ballInfoEntity.getFinal_score() + ",");
                            }
                            break;
                        case "3006": //让球胜平负
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                if (!key2.equals("single")) {
                                    if (key2.equals("0")) {
                                        stringBufferValue.append("让负,");
                                    }
                                    if (key2.equals("1")) {
                                        stringBufferValue.append("让平,");
                                    }
                                    if (key2.equals("3")) {
                                        stringBufferValue.append("让胜,");
                                    }
                                }
                            }
//                            if (ballInfoEntity.getFinal_score() != null || "null".equals(ballInfoEntity.getFinal_score())) {
//                                if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) > Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
//                                    //主胜
//                                    kaijiang.append("让胜,");
//                                } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) == Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
//                                    //主胜
//                                    kaijiang.append("让平,");
//                                } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) < Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
//                                    //主胜
//                                    kaijiang.append("让负,");
//                                }
//                            }
                            if (!TextUtils.isEmpty(ballInfoEntity.getLet_score())) {
                                kaijiang.append(ballInfoEntity.getFinal_score() + ",");
                            }
                            break;
                        case "3008":
                            //总进球
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                if (key2.equals("goals")) {
                                    try {
                                        JSONObject jsonObject2 = jsonObjectValue.getJSONObject(key2);
                                        Iterator<String> iterator1 = jsonObject2.keys();
                                        while (iterator1.hasNext()) {
                                            String key3 = iterator1.next();
                                            stringBufferValue.append(key3 + "球,");
                                        }
                                    } catch (Exception e) {

                                    }

                                }
                            }
                            if (!TextUtils.isEmpty(ballInfoEntity.getFinal_score())) {
                                kaijiang.append(Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) + Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1]) + "球,");
                            }
                            break;
                        case "3009":
                            //半全场
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                if (!key2.equals("single")) {
                                    stringBufferValue.append(key2.replaceAll("3", "胜").replaceAll("1", "平").replaceAll("0", "负") + ",");
                                }
                            }
                            break;
                        //-----------篮球我的推荐
                        case "3001"://胜负
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String sfValue = "";
                                if (key2.equals("0")) {
                                    sfValue = "让客胜";
                                }
                                if (key2.equals("3")) {
                                    sfValue = "让主胜";
                                }
                                if (sf_tag.equals(key2) && !TextUtils.isEmpty(sfValue)) {
                                    sf = Const.Companion.getHtmlHead() + sfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + sfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.equals("0") || (key2.equals("3"))) {
                                    stringBufferValue.append(sfValue + "(" + value + ")<br />");
                                }
//                                if (!TextUtils.isEmpty(sfValue)) {
//
//                                }
                            }
                            break;
                        case "3002"://让胜负
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String rsfValue = "";
                                if (key2.equals("0")) {
                                    rsfValue = "客胜";
                                }
                                if (key2.equals("3")) {
                                    rsfValue = "主胜";
                                }
                                if (rsfTag.equals(key2) && !TextUtils.isEmpty(rsfValue)) {
                                    rsf = Const.Companion.getHtmlHead() + rsfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + rsfValue + "(" + value + ")" + "<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.equals("0") || (key2.equals("3"))) {
                                    stringBufferValue.append(rsfValue + "(" + value + ")" + "<br />");
                                }
                            }
                            break;
                        case "3003": //胜负差
                            boolean hasAdLHead = false, hasAdWHead = false;
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String sfcValue = "";
                                if (key2.startsWith("l")) {
                                    if (!hasAdLHead) {
                                        hasAdLHead = true;
                                        sfcValue = "客胜";
                                        stringBufferValue.append("客胜");
                                    }
                                    if (key2.equals("l1")) {
                                        sfcValue = "[1-5]";
                                    }
                                    if (key2.equals("l2")) {
                                        sfcValue = "[6-10]";
                                    }
                                    if (key2.equals("l3")) {
                                        sfcValue = "[11-15]";
                                    }
                                    if (key2.equals("l4")) {
                                        sfcValue = "[16-20]";
                                    }
                                    if (key2.equals("l5")) {
                                        sfcValue = "[21-25]";
                                    }
                                    if (key2.equals("l6")) {
                                        sfcValue = "[26+]";
                                    }
                                } else if (key2.startsWith("w")) {
                                    if (!hasAdWHead) {
                                        hasAdWHead = true;
                                        stringBufferValue.append("主胜");
                                        sfcValue = "主胜";
                                    }
                                    if (key2.equals("w1")) {
                                        sfcValue = "[1-5]";
                                    }
                                    if (key2.equals("w2")) {
                                        sfcValue = "[6-10]";
                                    }
                                    if (key2.equals("w3")) {

                                        sfcValue = "[11-15]";
                                    }
                                    if (key2.equals("w4")) {

                                        sfcValue = "[16-20]";
                                    }
                                    if (key2.equals("w5")) {
                                        sfcValue = "[21-25]";
                                    }
                                    if (key2.equals("w6")) {
                                        sfcValue = "[26+]";
                                    }
                                }

                                if (sfc_tag.equals(key2) && !TextUtils.isEmpty(sfcValue)) {
                                    sfc = Const.Companion.getHtmlHead() + (hasAdWHead ? "主胜" : "客胜") + sfcValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + sfcValue.replace(",", "") + "(" + value + ")" + "<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.startsWith("l") || (key2.startsWith("w"))) {
                                    stringBufferValue.append(sfcValue + "(" + value + ")<br />");
                                }
//                                if (!TextUtils.isEmpty(sfcValue)) {
//                                    stringBufferValue.append(",");
//                                }
                            }
                            break;
                        case "3004":
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String dxfValue = "";
                                if (key2.equals("0")) {
                                    dxfValue = "小分";
                                }
                                if (key2.equals("1")) {
                                    dxfValue = "大分";
                                }

                                if (dxf_tag.equals(key2) && !TextUtils.isEmpty(dxfValue)) {
                                    dxf = Const.Companion.getHtmlHead() + dxfValue.replace(",", "") + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + dxfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.equals("0") || (key2.equals("1"))) {
                                    stringBufferValue.append(dxfValue + "(" + value + ")<br />");
                                }
                                //预设总分
                                if (key2.equals("score")) {
                                    stringBufferValue.append("预设分(" + value + ")<br />");
                                }
                            }
                            break;
                    }
                    values[0] += (stringBufferValue.toString());
                    if (type == 2) {//篮球
                        values[1] = getLqPrize(ballInfoEntity.getBasketball_info());
                    } else {
                        values[1] = (kaijiang.toString().endsWith(",") ? (kaijiang.toString().substring(0, kaijiang.length() - 1)) : kaijiang.toString());
                    }
                    values[3] = key;
//                    break;
                }
            } else {
                values[0] = ("");
            }
            values[0] = (values[0].endsWith(",") ? (values[0].substring(0, values[0].length() - 1)) : values[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return values;
    }

    public static String getLqPrize(BallInfoEntity.BascketBallInfo bascketball_info) {
        StringBuilder stringBuilder = new StringBuilder();
        if (bascketball_info != null) {
            String hdc = bascketball_info.getHdc();//让分
            stringBuilder.append(getHdc(hdc) + "<br />");
            String mnl = bascketball_info.getMnl();//非让分
            stringBuilder.append(getMnl(mnl) + "<br />");
            String wnm = bascketball_info.getWnm();//胜分差
            stringBuilder.append(getWnm(wnm) + "<br />");
            String hilo = bascketball_info.getHilo();//大小分
            stringBuilder.append(getHilo(hilo));
        }
        return stringBuilder.toString();
    }
}
